import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import PersonCenter from '@/views/personCenter'
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [{
      path: '/redirect/:path(.*)',
      component: () => import('@/views/redirect/index')
    }]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'el-icon-s-home' }
      },
      {
        path: '/studyLine/:id',
        name: 'StudyLine',
        component: () => import('@/views/dashboard/view/studyLine'),
        meta: { title: '学习路线', icon: 'el-icon-s-home' }
      },
      {
        path: '/learnCenter',
        name: 'LearnCenter',
        component: () => import('@/views/learnCenter/index'),
        meta: { title: '课程中心', icon: 'el-icon-s-home' }
      },
      {
        path: '/courseBody/:id',
        name: 'CourseBody',
        component: () => import('@/views/learnCenter/view/courseBody'),
        meta: { title: '课程详情', icon: 'el-icon-s-home' }
      },
      {
        path: '/playVideo/:id',
        name: 'PlayVideo',
        component: () => import('@/views/learnCenter/view/playVideo'),
        meta: { title: '播放视频', icon: 'el-icon-s-home' }
      },
      {
        path: '/questionCenter',
        name: 'QuestionCenter',
        component: () => import('@/views/questionCenter/index'),
        meta: { title: '知识区', icon: 'el-icon-s-home' }
      },
      {
        path: '/askQuestions/:id',
        name: 'AskQuestions',
        component: () => import('@/views/questionCenter/view/askQuestions'),
        meta: { title: '我要提问', icon: 'el-icon-s-home' }
      },
      {
        path: '/questionDetail/:id',
        name: 'QuestionDetail',
        component: () => import('@/views/questionCenter/view/questionDetail'),
        meta: { title: '问答详情', icon: 'el-icon-s-home' }
      },
      {
        path: '/personCenter',
        name: 'PersonCenter',
        component: PersonCenter,
        meta: { title: '个人中心', icon: 'el-icon-s-home' },
        redirect: '/personCenter/myCourses',
        children: [
          {
            path: '/personCenter/myCourses',
            name: 'MyCourses',
            component: () => import('@/views/personCenter/view/myCourses'),
            meta: { title: '我的课程', icon: 'el-icon-s-home',
              activeMenu: '/personCenter' }
          },
          {
            path: '/personCenter/myAsk',
            name: 'MyAsk',
            component: () => import('@/views/personCenter/view/myAsk'),
            meta: { title: '我的回答', icon: 'el-icon-s-home',
              activeMenu: '/personCenter' }
          },
          {
            path: '/personCenter/courseManage',
            name: 'CourseManage',
            component: () => import('@/views/personCenter/view/courseManage'),
            meta: { title: '课程管理', icon: 'el-icon-s-home',
              activeMenu: '/personCenter' }
          },
          {
            path: '/personCenter/courseOperate/:id',
            name: 'CourseOperate',
            component: () => import('@/views/personCenter/view/courseOperate'),
            meta: { title: '课程操作', icon: 'el-icon-s-home',
              activeMenu: '/personCenter', activeLeftMenu: 'CourseManage' }
          },
          {
            path: '/personCenter/askManage',
            name: 'AskManage',
            component: () => import('@/views/personCenter/view/askManage'),
            meta: { title: '帖子管理', icon: 'el-icon-s-home',
              activeMenu: '/personCenter' }
          },
          {
            path: '/personCenter/labelManage',
            name: 'LabelManage',
            component: () => import('@/views/personCenter/view/labelManage'),
            meta: { title: '标签管理', icon: 'el-icon-s-home',
              activeMenu: '/personCenter' }
          },
          {
            path: '/personCenter/typeManage',
            name: 'TypeManage',
            component: () => import('@/views/personCenter/view/typeManage'),
            meta: { title: '分类管理', icon: 'el-icon-s-home',
              activeMenu: '/personCenter' }
          },
          {
            path: '/personCenter/courseOrder/:id',
            name: 'CourseOrder',
            component: () => import('@/views/personCenter/view/courseOrder'),
            meta: { title: '学习路线排序', icon: 'el-icon-s-home',
              activeMenu: '/personCenter', activeLeftMenu: 'TypeManage' }
          },
          {
            path: '/personCenter/questionType',
            name: 'QuestionType',
            component: () => import('@/views/personCenter/view/questionType'),
            meta: { title: '问答分类', icon: 'el-icon-s-home',
              activeMenu: '/personCenter' }
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
    meta: {
      title: '用户登入'
    }
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

const createRouter = () => new Router({
  scrollBehavior: (to, from, savedPosition) => {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },
  // __MICRO_APP_BASE_ROUTE__ 为micro-app传入的基础路由
  base: window.__MICRO_APP_BASE_ROUTE__ || process.env.BASE_URL,
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
  router.push(`/login`)
}
export default router
